﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using WebProject.Core.Service;

namespace WebProject.Controllers
{
    public class HomeController : Controller
    {
        #region [Private Attributes]
        private readonly IProductionTypeService m_ProductionTypeService;
        private readonly IProductionService m_ProductionService;
        #endregion

        #region [Constructor]
        public HomeController(IProductionTypeService productionTypeService, IProductionService productionService)
        {
            if(productionService == null)
                throw new ArgumentNullException("productionService");
            if (productionTypeService == null)
                throw new ArgumentNullException("productionTypeService");
            m_ProductionTypeService = productionTypeService;
            m_ProductionService = productionService;
        }
        #endregion

        #region [Actions]
        public ActionResult Index()
        {
            /*
             * menu集合规则：
             * key: '类型_id'的组合
             * value: 'name'域
             */
            var menu = new Dictionary<string, string>();
            var productionTypes = m_ProductionTypeService.GetAllType();
            if (productionTypes != null && productionTypes.Count > 0)
            {
                foreach (var type in productionTypes)
                {
                    menu.Add("Type_" + type.Id, type.TypeName);
                    var productions = m_ProductionService.GetProductionsByType(type);
                    if (productions != null && productions.Count > 0)
                    {
                        foreach (var production in productions)
                        {
                            menu.Add("Production_" + production.Id, production.ProductionName);
                        }
                    }
                }
            }
            ViewBag.Menu = menu;
            /*
             * 首页显示4个主流产品
             */
            var productionsOnIndex = m_ProductionService.GetAllProductions(true);
            if (productionsOnIndex != null && productionsOnIndex.Count > 0)
            {
                ViewBag.Productions = productionsOnIndex;
            }
            return View();
        }
        #endregion
    }
}
